import time
import streamlit as st
import re
import data.data as dd

st.set_page_config(
    page_title="私人助手注册页面",
    page_icon='😍'
)
st.title("私人助手注册页面")
username = st.text_input('请输入用户名')
password = st.text_input('请输入密码',type='password')
re_password = st.text_input('请重复输入密码',type='password')
register_flag = st.button("注册")
login_flag = st.button("已有账号？点击此登录")



def register(username, password, re_password):

    if username and password and re_password:

        if re.match('^(13|15|17|18|19)[0-9]{9}$', username):

            if password == re_password and len(password) >= 8:

                if dd.query_user_no_exists(username):
                    dd.add_user(username, password)
                    st.success('注册成功')
                    time.sleep(2)
                    st.switch_page('login.py')
                else:
                    st.error('用户已经存在，请登录')
            else:
                st.error("两次密码不一致或者长度字段不足八位")
        else:
            st.error('您的手机号码格式不正确')

    else:
        st.error("请务必填写相关注册信息！")

if register_flag:
    register(username, password, re_password)

if login_flag:
    st.switch_page('login.py')
